<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        .login-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .login-container input {
            width: 100%;
            padding: 10px;
            margin-bottom: 15px;
            border: 1px solid #ccc;
            border-radius: 3px;
            box-sizing: border-box;
        }

        .login-container button {
            width: 100%;
            padding: 10px;
            background-color: #007BFF;
            color: #fff;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .login-container button:hover {
            background-color: #0056b3;
        }
    </style>
</head>

<body>
    <div class="login-container">
        <h2>登录</h2>
        <form action="#" id="loginForm">
            <input type="text" placeholder="用户名" required>
            <input type="password" placeholder="密码" required>
            <button type="submit">登录</button>
        </form>
    </div>
    <script>
        const form = document.getElementById('loginForm');

        form.addEventListener('submit', async function (e) {
            e.preventDefault();
            const username = form.querySelector('input[type="text"]').value;
            const password = form.querySelector('input[type="password"]').value;

            try {
                const response = await fetch('/api/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        username: username,
                        password: password
                    })
                });

                if (response.ok) {
                    const data = await response.json();
                    // 将 token 存入 localStorage
                    if(data.status == 'success'){
                        console.log('登录成功，token:', data.token);
                        localStorage.setItem('token', data.token);
                    // 假设登录成功后跳转到 index.html
                    window.location.href = "index.html";
                    }else {
                        alert('用户名或密码错误，请重试。');
                    }
                } else {
                    alert('用户名或密码错误，请重试。');
                }
            } catch (error) {
                console.error('登录请求出错:', error);
                alert('登录请求出错，请稍后重试。');
            }
        });

    </script>
</body>

</html>    